package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.Event.EventTest;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.adapter.MyAdapter;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.InfoType;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.NoScrollView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ChooseInfoTypeActivity extends AbsActivity {
    private TextView topTitle;
    private ImageView icBack;
    private RecyclerView recyclerView;
    private ChooseAdapter chooseAdapter;
    private InfoType nowInfo;
    private List<InfoType> data = new LinkedList<>();

    @Override
    public int setView() {
        return R.layout.activity_choose_info_type;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        nowInfo = (InfoType) getIntent().getSerializableExtra("data");
        if(nowInfo!=null && nowInfo.getSon().size()!=0){
            data.addAll(nowInfo.getSon());
        }
        initView();
        initLogic();
    }

    private void initLogic() {
        chooseAdapter = new ChooseAdapter(R.layout.info_type_item,data);
        recyclerView.setAdapter(chooseAdapter);
        icBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        topTitle = findViewById(R.id.title);
        icBack = findViewById(R.id.ic_back);
        recyclerView = findViewById(R.id.recycleview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        icBack.setVisibility(View.VISIBLE);
        topTitle.setText("选择信息类别");
    }

    @Subscribe
    public void onEventMainThread(EventTest event) {
        if(event.getCode() == 1) finish();
    }

    private class ChooseAdapter extends BaseQuickAdapter<InfoType,BaseViewHolder>{

        public ChooseAdapter(int layoutResId, @Nullable List<InfoType> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final InfoType item) {
            helper.setText(R.id.tv_title,item.getName());
            NoScrollView gv = helper.getView(R.id.gv);
            final List<InfoType> da = item.getSon();
            ArrayList<String> data = new ArrayList<>();
            if(da!=null && da.size()!=0){
                for(InfoType info:da){
                    data.add(info.getName());
                }
            }

            gv.setAdapter(new MyAdapter<String>(data,R.layout.info_type_item2) {
                @Override
                public void bindView(final ViewHolder holder, final String obj) {
                    holder.setText(R.id.tv_subtype,obj);
                    holder.setOnClickListener(R.id.tv_subtype, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ToastUtil.showToast(da.get(holder.getItemPosition()).getName());
                            InfoType type = da.get(holder.getItemPosition());
                            type.setInfoString(nowInfo.getName()+"->"+item.getName()+"->"+type.getName());
                            startActivity(new Intent(mActivity,PublishInfoActivity.class).putExtra("data",da.get(holder.getItemPosition())));
                        }
                    });
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
